<?php
namespace classlib\core\view;
class execl extends view_abstract {
	private $smart;
	private $file;
	
	function __construct($params=null) {
		$this->smart = array('data'=>array(), 'header'=>array(), 'title'=>'');
		$this->file  = '';
	}

	/**
	 * 变量赋值
	 * @param string $key
	 * @param mix    $val
	 * @return view_abstract
	 */
	public function assign($key, $val) {
		$this->smart[$key] = $val;
		return $this;
	}
	
	/**
	 * 变量赋值引用赋值
	 * @param string $key
	 * @param mix $val
	 * @return view_abstract
	 */
	public function assign_by_ref($key, &$val) {
		$this->smart[$key] = &$val;
		return $this;
	}

	/**
	 * 已经分配的变量的获取 默认取所有
	 * @param string $key
	 * @return mix
	 */
	public function get_template_vars($key=false) {
		if($key) {
			$data = isset($this->smart[$key])? $this->smart[$key]:false;
			return $data;
		}
		return $this->smart;
	}
	
	/**
	 * 将要输出的资料获取
	 * @param string  $tpl
	 * @param boolean $iscache
	 * @return string
	 */
	public function fetch($tpl=false, $iscache=false) {
		$ua       	= $_SERVER['HTTP_USER_AGENT'];
		if(empty($this->file)) {
			$this->file = date('YmdHis');
		}
		$file       = $this->file;
        $this->file = urlencode($this->file);
        $this->file = str_replace('+', '%20', $this->file);
        header("Content-type:application/vnd.ms-excel");
        if(preg_match("/MSIE/i", $ua)) {
            header('Content-Disposition: attachment; filename="'.$this->file.'.xls"');
        } else if(preg_match("/Firefox/i", $ua)) {
            header("Content-Disposition: attachment; filename*=\"utf8''".$file.'.xls"');
        } else {
            header('Content-Disposition: attachment; filename="'.$file.'.xls"');
        }
	    $html ='<html xmlns:o="urn:schemas-microsoft-com:office:office"
	       xmlns:x="urn:schemas-microsoft-com:office:excel"
	       xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
	       <head>
	       <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
	       <meta http-equiv=Content-Type content="text/html; charset="utf-8">
	       <!--[if gte mso 9]><xml>
	       <x:ExcelWorkbook>
	       <x:ExcelWorksheets>
	               <x:ExcelWorksheet>
	               <x:Name></x:Name>
	               <x:WorksheetOptions>
	                       <x:DisplayGridlines/>
	               </x:WorksheetOptions>
	               </x:ExcelWorksheet>
	       </x:ExcelWorksheets>
	       </x:ExcelWorkbook>
	       </xml><![endif]-->
	       </head>
	    	<body link=blue vlink=purple leftmargin=0 topmargin=0>';
	    $html .='<table width="100%" border="0" cellspacing="0" cellpadding="0">';
	    $nlen  = count($this->smart['header']);
	    if($this->smart['title']) {
	     	$html .='<tr><td align="center" colspan="'.$nlen.'">'.$this->smart['title'].'</td></tr>';
	    }
		$html .='<tr>';
        foreach($this->smart['header'] as $key=>$val){
            $html .='<td>'.$val.'</td>';
        }
        $html .='</tr>';
		if (!empty($this->smart['data'])) {
		    foreach($this->smart['data'] as &$item) {
		    	$html .='<tr>';
		    	foreach($this->smart['header'] as $key=>$val) {
		    		$html .='<td align="left">'.$item[$key].'</td>';
		    	}
			    $html .='</tr>';
		    }
		}
	    $html .='</table>';
	    $html .='</body>';
	    $html .='</html>';
	    return $html;
	}

	/**
	 * 重载Smarty输出的资料输出
	 * @param string  $tpl
	 * @param int     $caching 
	 * @param string  $cacheid  区别同一个页面不同展示模版
	 * @param int     $lifetime
	 * @see Smarty  $caching
	 */
	public function display($tpl=false, $caching=0, $cacheid=null, $lifetime=3600) {
		$debug = parent::display();
		$this->assign_by_ref('debug', $debug);
		echo $this->fetch($tpl);
	}
}